package com.haozhen

import scala.io.StdIn
import scala.util.Random
import scala.util.control.Breaks

/**
  * @author haozhen
  * @email haozh@ync1.com
  * @date 2020/12/8  21:22
  *
  *      1.2 作业分析
  *       分析业务逻辑，抽象出类、类的属性和方法，如下：
  *         1. 创建用户类User，定义类的属性（name，score）和类的方法（showFist())
  *         2. 创建计算机类Computer，定义类的属性（name，score）和类的方法（showFist())
  *         3. 实现计算机随机出拳
  *         4. 创建游戏类Game，定义类的属性（甲方玩家、乙方玩家、对战次数）
  *         5. 编写初始化方法、游戏开始方法
  */

class Player(val name:String){
  var score:Int=_
}

class GuessingGame(val player:Player) {

  val roles =("刘备","关羽","张飞")

//  var sRole :String =_
  var npcPlayer:Player=_


  val box =("剪刀","石头","布")

  var winTimes:Int=_

  var peaceTimes:Int=_

  var times = 0;

  def toSelectRole(): Unit ={
    println("请选择对战角色：(1.刘备 2.关羽 3.张飞)")
    var selectRole = 0
    Breaks.breakable(
      while(true){
        val role = StdIn.readLine()
        try {
          selectRole = role.toInt
          if(selectRole==1 || selectRole==2 ||selectRole==3){
            Breaks.break()
          }else{
            throw new Exception("输入数据无效")
          }
        }catch {
          case _:Exception =>println("请重新输入  1 , 2, 3 其他输入无效")
        }
      }
    )
    val sRole = roles.productElement(selectRole-1).asInstanceOf[String]
    npcPlayer = new Player(sRole)
    println(s"你选择了与 ${npcPlayer.name}作战")
  }

  def isStart(): Boolean ={

    var start = ""
    Breaks.breakable(
      while(true){
         start = StdIn.readLine()
          if(start=="y"|| start=="n"){
            Breaks.break()
          }else{
            println("请重新输入  y/n 其他输入无效")
          }
      }
    )
    if(start=="y"){
       true
    }else{
       false
    }
  }

  def guess(): Unit ={
    println("请出拳！ 1.剪刀 2.石头 3.布")
    var card = 0
     var flag = true
      val role = StdIn.readLine()
      try {
        card = role.toInt
        if(card==1 || card==2 ||card==3){
        }else{
          throw new Exception("输入数据无效")
        }
      }catch {
        case _:Exception =>card =3
          flag = false
      }
    if(flag) {
      println(s"你出拳 ${box.productElement(card-1)}")
    }else{
      println("输入不符合规则，默认出布")
    }
   val roleCard =  Random.nextInt(3)+1
    println(s"${npcPlayer.name}  出拳 ${box.productElement(roleCard-1)}")
    if(card == roleCard){
      player.score+=1
      npcPlayer.score+=1
      peaceTimes+=1
      println("结果：和局！下次继续努力！")
    }else if(card!=1 && roleCard<card){
      player.score+=2
      winTimes+=1
      println("结果：恭喜，你赢啦！")
    }else if(card==1 && roleCard==3){
      player.score+=2
      winTimes+=1
      println("结果：恭喜，你赢啦！")
    }else{
      npcPlayer.score+=2

      println("结果：抱歉，你输啦！")
    }
    times+=1
    println("是否进入下一轮(y/n)")
    if(isStart()){
      guess()
    }
  }

  def toEnd(): Unit ={
    println("退出游戏")
    println("---------------------------------")
    println(s"${npcPlayer.name}  VS 游客")
    println(s"对战次数 ${times}次")
    println("姓名\t得分\t胜局\t和局\t负局")
    println(s"游客\t\t${player.score}\t\t${winTimes}\t\t${peaceTimes}\t\t${times-peaceTimes-winTimes}")
    println(s"${npcPlayer.name}\t\t${npcPlayer.score}\t\t${times-peaceTimes-winTimes}\t\t${peaceTimes}\t\t${winTimes}")
  }

}
object GuessingGame{
  def main(args: Array[String]): Unit = {

    println("-----------------欢迎进入游戏时间--------------------")
    println("*****************************************************")
    println("**********************猜拳，开始************************")
    println("*****************************************************")
    println()
    println()
    val guessingGame = new GuessingGame(new Player("游客"))
    guessingGame.toSelectRole()
    println("要开始么？y/n")
    if(guessingGame.isStart()){
      guessingGame.guess()
    }
    guessingGame.toEnd()
  }
}
